package com.linkage.bookshop.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linkage.bookshop.model.Book;
import com.linkage.bookshop.model.Booktype;
import com.linkage.bookshop.service.inter.BookService;
import com.linkage.bookshop.repository.BookRepository;

@Service("bookService")
public class BookServiceImpl implements BookService {

	@Autowired
	private BookRepository bookRepository;
	
	@Override
	public List<Book> listBooks(int typeId, int numPerPage, int pageNum,
			String orderBy) {
		int offset = 0;
		int rows =10;
		
		if (null == this.bookRepository.selectBookTypeById(typeId)){
			return null;
		}
		if (numPerPage <= 0 || pageNum <= 0){
			return null;
		}
		
		offset = offset + numPerPage * (pageNum - 1);
		rows = numPerPage;
		return this.bookRepository.selectBooksByTypeId(typeId, offset, rows, orderBy);
	}

	@Override
	public List<Book> listBooks(String keyWord, int numPerPage, int pageNum,
			String orderBy) {
		int offset = 0;
		int rows =10;
		
		if (numPerPage <= 0 || pageNum <= 0){
			return null;
		}
		
		offset = offset + numPerPage * (pageNum - 1);
		rows = numPerPage;
		return this.bookRepository.selectBooksByKeyWord(keyWord, offset, rows, orderBy); 
	}

	@Override
	public List<Book> listAllBooksOnsale(int typeId){
		return this.bookRepository.selectAllBooksOnsaleByTypeId(typeId);
	}
	
	@Override 
	public List<Book> listAllBooksOnsale(String keyWord){
		return this.bookRepository.selectAllBooksOnsaleByKeyWord(keyWord);
	}
	
	@Override
	public List<Book> listAllBooks(int typeId){
		return this.bookRepository.selectAllBooksByTypeId(typeId);
	}
	
	@Override
	public List<Book> listAllBooks(String keyWord){
		return this.bookRepository.selectAllBooksByKeyWord(keyWord);
	}
	
	@Override
	public List<Book> listAllBooks(){
		return bookRepository.selectAllBooks();
	}
	
	@Override
	public Book loadBookDetail(int bookId) {
		return this.bookRepository.selectBookDetail(bookId);
	}

	@Override
	public boolean addType(String typeName) {
		if ("" == typeName){
			return false;
		}
		if (null != this.bookRepository.selectBookTypeByName(typeName)){
			return false;
		}
		
		Booktype bookType = new Booktype();
		bookType.setName(typeName);
		this.bookRepository.insertType(bookType);
		return true;
	}

	@Override
	public List<Booktype> listAllTypes() {
		return this.bookRepository.selectAllTypes(); 
	}

	@Override
	public boolean addBook(Book book) {
		
		if (null == book.getBooktype()
				|| null == book.getName() || null == book.getAuthor()
				|| null == book.getOutPrice() || null == book.getInPrice()
				|| null == book.getPress() || null == book.getPublishTime()
				|| null == book.getSaleVolume() || null == book.getStorageVolume()){
			return false;
		}
		if (null == this.bookRepository.selectBookTypeById(book.getBooktype().getId())){
			return false;
		}
		
		this.bookRepository.insertBook(book);
		return true;
	}

	@Override
	public boolean modifyBook(Book book) {
		if (null == book.getId()){
			return false;
		}

		this.bookRepository.updateOnsaleFlag(book.isOnsaleFlag(), book.getId());

		if (null != book.getBooktype()){
			this.bookRepository.updateBookType(book.getBooktype().getId(), book.getId());
		}
		if (null != book.getName()){
			this.bookRepository.updateBookName(book.getName(), book.getId());
		}
		if (null != book.getAuthor()){
			this.bookRepository.updateAuthor(book.getAuthor(), book.getId());
		}
		if (null != book.getOutPrice()){
			this.bookRepository.updateOutPrice(book.getOutPrice(), book.getId());
		}
		if (null != book.getPress()){
			this.bookRepository.updatePress(book.getPress(), book.getId());
		}
		if (null != book.getPublishTime()){
			this.bookRepository.updatePublishTime(book.getPublishTime(), book.getId());
		}
		if (null != book.getDescription()){
			this.bookRepository.updateDescription(book.getDescription(), book.getId());
		}
		if (null != book.getImagePath()){
			this.bookRepository.updateImagePath(book.getImagePath(), book.getId());
		}
		if (null != book.getStorageVolume()){
			this.bookRepository.updateStorageVolume(book.getStorageVolume(), book.getId());
		}
		
		
		return true;
	}

	@Override
	public boolean pushBook(int bookId) {
		Book book = new Book();
		book = this.bookRepository.selectBookDetail(bookId);
		if (null == book){
			return false;
		}
		if (book.isOnsaleFlag() == true){
			return false;
		}else{
			this.bookRepository.updateBookStatusOnsale(bookId);
		}
		return true;
	}
	
	@Override
	public boolean pullBook(int bookId) {
		Book book = new Book();
		book = this.bookRepository.selectBookDetail(bookId);
		if (null == book){
			return false;
		}
		if (book.isOnsaleFlag() == false){
			return false;
		}else{
			this.bookRepository.updateBookStatusNotOnsale(bookId);
		}
		return true;
	}

	@Override
	public boolean checkTypeName(String typeName){
		if (null == this.bookRepository.selectBookTypeByName(typeName)){
			return true;
		}
		return false;
	}
	
	@Override
	public boolean modifyVolume(int bookId, int num, boolean flag){
		Book bookOut = this.bookRepository.selectBookDetail(bookId);
		int saleVolume = bookOut.getSaleVolume();
		int storageVolume = bookOut.getStorageVolume();
		if (flag == true){
			if (num > storageVolume){
				return false;
			}
			
			storageVolume = storageVolume - num;
			saleVolume = saleVolume + num;
			this.bookRepository.updateStorageVolume(storageVolume, bookId);
			this.bookRepository.updateSaleVolume(saleVolume, bookId);
		}else{
			if (num > saleVolume){
				return false;
			}
			storageVolume = storageVolume + num;
			saleVolume = saleVolume - num;
			this.bookRepository.updateStorageVolume(storageVolume, bookId);
			this.bookRepository.updateSaleVolume(saleVolume, bookId);
		}
		return true;
	}
}
